package com.icefish.daoImpl;

import com.icefish.bean.Shopping_cart;
import com.icefish.bean.fastjsonParse.addShopCartToOrders.InsertReturnInfo;
import com.icefish.dao.Shopping_cartDao;
import com.icefish.utils.DatabaseConnFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class Shopping_cartDaoImpl implements Shopping_cartDao {
    @Override
    public boolean insertGoodTOCart(Shopping_cart shopping_cart)throws Exception {

        String sql="insert into shopping_cart(shopping_count,shopping_amount,goods_id,goods_name,goods_price,goods_picture,goods_describe,user_id,username) values(?,?,?,?,?,?,?,?,?)";
        Connection conn = DatabaseConnFactory.getConn();

        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setInt(1,shopping_cart.getShopping_count());
        psmt.setInt(2,shopping_cart.getShopping_amount());
        psmt.setInt(3,shopping_cart.getGoods_id());
        psmt.setString(4,shopping_cart.getGoods_name());
        psmt.setInt(5,shopping_cart.getGoods_price());
        psmt.setString(6,shopping_cart.getGoods_picture());
        psmt.setString(7,shopping_cart.getGoods_describe());
        psmt.setInt(8,shopping_cart.getUser_id());
        psmt.setString(9,shopping_cart.getUsername());

        int up = psmt.executeUpdate();

        // 关闭连接，释放资源
        if (psmt != null) {
            psmt.close();
        }
        if (conn != null) {
            conn.close();
        }

        return ( up == 1);

    }

    @Override
    public InsertReturnInfo selectReturnInfo(Integer goodId, Integer userId) throws Exception {

        String sql="select shopping_cart_id,goods_id,user_id from shopping_cart where goods_id=? and user_id=?";
        Connection conn = DatabaseConnFactory.getConn();

        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setInt(1,goodId);
        psmt.setInt(2,userId);

        ResultSet rst = psmt.executeQuery();

        InsertReturnInfo info = new InsertReturnInfo();

        while (rst.next()){
            info.setShopping_cart_id(rst.getInt("shopping_cart_id"));
            info.setGoods_id(rst.getInt("goods_id"));
            info.setUser_id(rst.getInt("user_id"));

        }
        System.out.println(info.toString());

        // 关闭连接，释放资源
        if (rst != null) {
            psmt.close();
        }
        if (psmt != null) {
            psmt.close();
        }
        if (conn != null) {
            conn.close();
        }

        return info;
    }

    // 获取所以购物车数据，通过用户id
    public ArrayList<Shopping_cart> selectAllShopCartByUserId (Integer userId) throws Exception {

        String sql = "select * from shopping_cart where user_id = ?";
        Connection conn = DatabaseConnFactory.getConn();

        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setInt(1, userId);

        ResultSet rst = psmt.executeQuery();

        ArrayList <Shopping_cart> shoppingCarts = new ArrayList <Shopping_cart>();

        while (rst.next()) {

            Shopping_cart shoppingCart = new Shopping_cart();

            shoppingCart.setShopping_cart_id(rst.getInt("shopping_cart_id"));
            shoppingCart.setShopping_count(rst.getInt("shopping_count"));
            shoppingCart.setShopping_amount(rst.getInt("shopping_amount"));
            shoppingCart.setGoods_id(rst.getInt("goods_id"));
            shoppingCart.setGoods_name(rst.getString("goods_name"));
            shoppingCart.setGoods_price(rst.getInt("goods_price"));
            shoppingCart.setGoods_picture(rst.getString("goods_picture"));
            shoppingCart.setUser_id(rst.getInt("user_id"));
            shoppingCart.setUsername(rst.getString("username"));
            shoppingCart.setGoods_describe(rst.getString("goods_describe"));

            shoppingCarts.add(shoppingCart);

        }

        // 关闭连接，释放资源
        if (rst != null) {
            rst.close();
        }
        if (psmt != null) {
            psmt.close();
        }
        if (conn != null) {
            conn.close();
        }
        return  shoppingCarts;
    }

    @Override
    public boolean deleteById(Integer shopping_cart_id) throws Exception {

        String sql="DELETE FROM shopping_cart WHERE shopping_cart_id = ?";
        Connection conn = DatabaseConnFactory.getConn();

        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setInt(1, shopping_cart_id);

        int i =psmt.executeUpdate();

        conn.close();
        psmt.close();
        return (i == 1);
    }
}
